local LoginScene = class("LoginScene", cc.load("mvc").ViewBase)
local NameSettingLayer = require("app.layers.login_NameSettingLayer")
function LoginScene:onCreate()
    DyDataManager:init()
end

function LoginScene:onEnterTransitionFinish()
    self:initLoginUI()
end

function LoginScene:initLoginUI()
    local loginui=FZutil.createSceneNode("LoginUIall/LoginUIView")
    self:addChild(loginui.root)
    loginui.Node_center:setPosition(display.center)

    local last_account=cc.UserDefault:getInstance():getStringForKey("last_account")
    if last_account and last_account~="" then
        loginui.TextField_accountInput:setString(last_account)
    end
    
    local text_passward=loginui.TextField_passwordInput
    if device.platform=="windows" then
        text_passward:setPasswordEnabled(false)
        local last_port=cc.UserDefault:getInstance():getStringForKey("last_port")
        text_passward:setString(last_port)
        --text_passward:setString("0")
    end

    local btn=loginui.Button_login

--    local function clickfunc()
--        print("touch~~")
--        local current_account=loginui.TextField_accountInput:getString()
--        local current_port=text_passward:getString()
--        if string.len(current_account)==0 then
--            print("please enter account")
--        elseif string.len(current_port)==0 then
--            print("please enter port")
--        else
--            cc.UserDefault:getInstance():setStringForKey("last_account",current_account)
--            cc.UserDefault:getInstance():setStringForKey("last_port",current_port)
--            RoleData.loginname=current_account
--            if TEMP_SKIP_CONNECT then
--                loginui.root:hide()
--                self:enterGameScene()
--            else
--                NetEngine:connect("server.natappfree.cc",tonumber(current_port))
--            end
--        end
--    end
--    local function longPressFunc(sender,event)
--        print("long press")
--    end
--    local LongTouch = require("app.public.LongTouch")
--    LongTouch.registerLongPressHandler(btn,longPressFunc,clickfunc)
    btn:onTouch(function(event)
        if event.name== "ended" then
            print("touch~~")
            local current_account=loginui.TextField_accountInput:getString()
            local current_port=text_passward:getString()
            if string.len(current_account)==0 then
                print("please enter account")
            elseif string.len(current_port)==0 then
                print("please enter port")
            else
                cc.UserDefault:getInstance():setStringForKey("last_account",current_account)
                cc.UserDefault:getInstance():setStringForKey("last_port",current_port)
                RoleData.loginname=current_account
                if TEMP_SKIP_CONNECT then
                    loginui.root:hide()
                    self:enterGameScene()
                else
                    NetEngine:connect("server.natappfree.cc",tonumber(current_port))
                end
            end
        end
    end)
end

function LoginScene:enterGameScene()
    self:getApp():enterScene("StageScene")
end

function LoginScene:onLoginSuccess()
    self:enterGameScene()
end

function LoginScene:onCreatePlayer()
    self.nameSettingLayer = NameSettingLayer.new()
    self:addChild(self.nameSettingLayer)
--    local name=cc.UserDefault:getInstance():getStringForKey("last_account")
--    local msg={rolename=name,uid=RoleData.player_info.uid,sex=RoleData.player_info.sex}
--    NetEngine:sendMessage(ProtoCmd.ReqCreateRoleMessage,msg)
end

function LoginScene:onEnter()
    FzEventCenter:RegisterEvent(FzEvent.LoginSuccess,self,self.onLoginSuccess)
    FzEventCenter:RegisterEvent(FzEvent.CreatePlayer,self,self.onCreatePlayer)
end

function LoginScene:onExit()
    FzEventCenter:RemoveEvent(FzEvent.LoginSuccess,self)
    FzEventCenter:RemoveEvent(FzEvent.CreatePlayer,self)
end

return LoginScene